<template>
  <div class="block">
    <el-date-picker
      v-model="paramsQuery.time"
      type="month"
      value-format="yyyy-MM"
      @change="queryByTime"
      placeholder="审批日期">
    </el-date-picker>
    <el-button type="info" @click="newQuery" plain>清除选择</el-button>
    <br/>

    <el-col :span="1.5">
      <download-excel
        class="export-excel-wrapper"
        :data="excelList"
        :fields="json_fields"
        :header="excelTitle"
        name="部门请假信息.xls"

      >
        <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
        <el-button type="warning" icon="el-icon-download" size="mini" @click="getExportData">部门请假信息导出</el-button>
      </download-excel>
    </el-col>

    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column label="序号" type="index"/>
      <el-table-column label="部门名称" style="width:20%">
        <template slot-scope="scope">{{ scope.row.deptName}}</template>
      </el-table-column>
      <el-table-column label="请假次数" style="width:30%">
        <template slot-scope="scope">{{ scope.row.leaveCount}}</template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="paramsQuery.pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="paramsQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
    import {leaveCount} from "@/api/teacher/leaveCount/leave_count.js";

    export default {
        data() {
            return {
                tableData: [],
                total: 0,
                paramsQuery: {
                    pageNum: 1,
                    pageSize: 10,
                    time: ''
                },
                excelList: [],
                exportDataList: [],
                excelTitle: '部门请假信息',
                json_fields: {
                    "部门名称": "deptName",
                    "请假次数": "leaveCount",
                }
            }
        },
        created() {
            this.query()
        },
        methods: {
            handleSizeChange(val) {
                this.paramsQuery.pageSize = val;
                this.query();
            },
            handleCurrentChange(val) {
                this.paramsQuery.pageNum = val;
                this.query();
            },
            query() {
                leaveCount(this.paramsQuery).then(resp => {
                    this.tableData = resp.data.list;
                    this.total = resp.data.total;
                })
            },
            queryByTime(time) {
                let params = {
                    pageNum: 1,
                    pageSize: 10,
                    time: ""
                };
                params.time = time;
                leaveCount(params).then(resp => {
                    this.tableData = resp.data.list;
                    this.total = resp.data.total;
                })
            },
            newQuery() {
                this.query();
                this.paramsQuery.time = '';
            },
            getExportData() {
                leaveCount({pageNum: 1, pageSize: 10000, time:''}).then(response => {
                        this.excelList = [],
                            this.exportDataList = JSON.parse(JSON.stringify(response.data.list));
                        for (let i = 0; i < this.exportDataList.length; i++) {
                            this.excelList[i] = this.exportDataList[i]

                            this.excelList[i].createTime = this.parseTime(this.exportDataList[i].createTime)
                        }
                    }
                );
            }
        }
    }
</script>

<style scoped>

</style>
